package com.huli.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.List;

@Configuration
@EnableSwagger2
public class OrdersSwagger {

    @Bean
    public Docket getDocket() {
        //创建全局参数用于测试(每个请求都带一个header参数”X-Token”)
        List<Parameter> ps = Arrays.asList(
                new ParameterBuilder().required(false)
                        .modelRef(new ModelRef("string"))
                        .parameterType("header")
                        .name("X-Token")
                        .description("认证的token")
                        .build()
        );
        //创建文档的整体说明对象(作者、描述、标题、版本)
        ApiInfo af = new ApiInfoBuilder().contact(new Contact("李诗岸", "http://qq.com", "Squbirreland@overlook.com"))
                .description("Orders接口文档")
                .title("orders")
                .version("1.0")
                .build();
        //创建api文档对象设置类型
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        //返回文档对象并且设置相关信息
        return docket
                //添加整体说明信息
                .apiInfo(af)
                .select()
                //设置扫描哪些包下面的内容来生成接口文档
                //路径过滤，选择所有
                .paths(PathSelectors.any())
                .apis(RequestHandlerSelectors.basePackage("com.huli.controller"))
                .build()
                //添加全局参数
                .globalOperationParameters(ps);

    }

}

